package com.example.ai_basedintelligentdiagnosisandtreatmentofchildren.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.ai_basedintelligentdiagnosisandtreatmentofchildren.R;
import com.example.ai_basedintelligentdiagnosisandtreatmentofchildren.activity.doctor.ActivityHomeDoctor;
import com.example.ai_basedintelligentdiagnosisandtreatmentofchildren.activity.parent.ActivityHomeParent;
import com.example.ai_basedintelligentdiagnosisandtreatmentofchildren.databinding.ActivityMedicineFeedbackBinding;
import com.example.ai_basedintelligentdiagnosisandtreatmentofchildren.model.UserViewModel;
import com.example.ai_basedintelligentdiagnosisandtreatmentofchildren.utils.GlobalViewModelStore;
import com.example.ai_basedintelligentdiagnosisandtreatmentofchildren.utils.MyApplication;
import com.google.android.material.appbar.MaterialToolbar;

//用药反馈界面
public class ActivityMedicineFeedback extends AppCompatActivity {

    private ActivityMedicineFeedbackBinding binging;
    private UserViewModel userViewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        binging = ActivityMedicineFeedbackBinding.inflate(getLayoutInflater());
        setContentView(binging.getRoot());
        ViewCompat.setOnApplyWindowInsetsListener(binging.getRoot(), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        setUserViewModel();

        // 找到标题栏
        MaterialToolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        // 设置返回按钮可见
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setHomeButtonEnabled(true);
        }

        // 设置返回按钮点击监听器
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                navigateBackToHomeActivity();
            }
        });
    }

    private void setUserViewModel(){
        //用户身份
        // 获取全局共享的 UserViewModel
        userViewModel = GlobalViewModelStore
                .getInstance(MyApplication.getInstance())
                .get(UserViewModel.class);
    }

    private void navigateBackToHomeActivity() {
        var targetActivity = userViewModel.isParent()?ActivityHomeParent.class:ActivityHomeDoctor.class;
        // 创建Intent指向HomeParentActivity
        Intent intent = new Intent(this, targetActivity);

        // 设置FLAG_ACTIVITY_CLEAR_TOP标志，清除HomeParentActivity之上的所有Activity
        // 并设置FLAG_ACTIVITY_SINGLE_TOP标志，确保HomeParentActivity不会重新创建
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

        // 启动HomeParentActivity
        startActivity(intent);

        // 结束当前Activity
        finish();
    }
}